﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HRMS.Models;
using DataManager;
using HRMS_Model;
using Lib.Web.Mvc.JQuery.JqGrid;
using System.Globalization;
using HRMS.Models.ModelBinders;

namespace HRMS.Controllers
{
    public class JobSkillController : Controller
    { 
        //
        // GET: /JobSkill/

        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult JobSkills(JqGridRequest request)
        {
            //get the count of employees
            var totalRecords = JobSkillsModels.GetJobSkillsCount();

            //Prepare JqGridData instance
            var response = new JqGridResponse
            {
                //Total pages count
                TotalPagesCount = (int)Math.Ceiling((float)totalRecords / (float)request.RecordsCount),
                //Page number
                PageIndex = request.PageIndex,
                //Total records count
                TotalRecordsCount = totalRecords
            };

            //Get employees
            var jobSkills = JobSkillsModels.GetJobSkills(request.SortingName.ToString(CultureInfo.InvariantCulture), request.SortingOrder.ToString(), request.PageIndex, request.RecordsCount, request.PagesCount.HasValue ? request.PagesCount.Value : 1);

            //Table with rows data
            foreach (var jobSkill in jobSkills)
            {
                response.Records.Add(new JqGridRecord(Convert.ToString(jobSkill.JobSkillId),
                    new List<object> { 
                                  jobSkill.JobSkillId,
               jobSkill.Job.JobId,
                  jobSkill.Skill.SkillId  
                    }
                ));
            }

            //Return data as json
            return new JqGridJsonResult() { Data = response };
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertJobSkill(JobSkill jobModel)
        {
            return Json(JobSkillsModels.AddJobSkills(jobModel));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateJobSkill([ModelBinder(typeof(CustomModelBinder))]JobSkill jobModel)
        {
            return Json((JobSkillsModels.UpdateJobSkill(jobModel)));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DeleteJobSkills(int id)
        {
            return Json(JobSkillsModels.DeleteJobSkill(id));
        }


    }
}


